import {hrAjax as proFetch} from 'src/hrpub/common/utils/utils';
import {toast} from 'nc-lightapp-front';

export default function stopConConfirm() {
    const formData = this.props.form.getAllFormValue('ctrtoperatedlg');
    return proFetch({
        url: '/nccloud/hrcm/contopinion/OpinionRemcStopOkAction.do',
        body: {
            pk_opinions: this.state.stopPkOpinions,
            ctrtoperatedlg: formData
        }
    }).then((res) => {
        if (res.success) {
            if (res.success) {
                if (res.data.ctrtpks && res.data.ctrtpks.length) {
                    this.alertModalNode.show(
                        [this.state.json['cm6011-000048']],
                        [],
                        () => {
                            /* 国际化处理： 是否立即生效？*/
                            stopNow.call(this, res.data.ctrtpks);
                        },
                        () => {
                            this.setState({stopModalVisible: false});
                            this.getMainTableData();
                        }
                    );
                } else {
                    this.setState({stopModalVisible: false});
                    this.getMainTableData();
                }
            }
        }
    });
}

function stopNow(ctrtpks) {
    return proFetch({
        url: '/nccloud/hrcm/contopinion/OpinionRemcStopEffectiveAction.do',
        body: {
            ctrtpks
        }
    }).then((res) => {
        if (res.success) {
            if (res.data.psnSelListVO && res.data.psnSelListVO.agreementgrid) {
                this.alertModalNode.show(
                    [res.data.msg],
                    [],
                    () => {
                        this.setState({
                            stopAgreeVisible: true
                        }, () => {
                            this.props.editTable.setTableData('agreementgrid', {
                                rows: res.data.psnSelListVO.agreementgrid.rows
                            });
                            this.props.editTable.selectAllRows('agreementgrid', true)
                        })
                    },
                    () => {
                        toast({color: 'success', content: this.state.json['cm6011-000110']});
                        this.setState({stopModalVisible: false});
                        this.getMainTableData();
                    }
                );
            } else {
                toast({color: 'success', content: this.state.json['cm6011-000110']});
                this.setState({stopModalVisible: false});
                this.getMainTableData();
            }
        }
    });
}
